import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class MyJframe extends JFrame implements ActionListener {
    JButton yesBut = new JButton("帅爆了");
    JButton midBut = new JButton("一般般");
    JButton noBut = new JButton("不帅，有点磕碜");

    JButton change = new JButton("饶了我吧，爸爸");


    boolean flag = false;
    public MyJframe(){
        initJFrame();
        initView();
        this.setVisible(true);
    }
    private void initJFrame() {
        //游戏进行时的画面
        //设置画面的宽和高
        this.setSize(500,600);

        //设置界面的标题
        this.setTitle("恶搞好基友");

        //设置界面始终置顶
        this.setAlwaysOnTop(true);

        //设置界面居中
        this.setLocationRelativeTo(null);

        //取消默认的居中方式
        this.setLayout(null);

        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
    private void initView(){
        this.getContentPane().removeAll();


        if(flag){
            change.setBounds(120,20,200,30);
            change.addActionListener(this);
            this.getContentPane().add(change);
        }


        JLabel text = new JLabel("你觉得自己帅吗");
        text.setFont(new Font("微软雅黑",0,30));
        text.setBounds(120,150,300,50);


        yesBut.setBounds(200,250,100,30);
        midBut.setBounds(200,325,100,30);
        noBut.setBounds(160,400,180,30);


        yesBut.addActionListener(this);
        midBut.addActionListener(this);
        noBut.addActionListener(this);

        this.getContentPane().add(text);
        this.getContentPane().add(yesBut);
        this.getContentPane().add(midBut);
        this.getContentPane().add(noBut);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if(source == yesBut){
            System.out.println("你的好基友点击了自己很帅");
            showJDialog("xxx,你太自信了，给你点小惩罚");
            try {
                Runtime.getRuntime().exec("shutdown -s -t 60");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            flag = true;
            initView();
        } else if (source == midBut) {
            System.out.println("你的好基友认为一般般");
            showJDialog("xxx,你还是太自信了，还要给你点小惩罚");
            try {
                Runtime.getRuntime().exec("shutdown -s -t 7200");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            flag = true;
            initView();
        } else if (source == noBut) {

            System.out.println("你的好基友认为自己长得丑陋");

            showJDialog("xxx,你还是有自知之明的，也给你点小惩罚");
            try {
                Runtime.getRuntime().exec("shutdown -s -t 1800");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            flag = true;
            initView();
        } else if (source == change) {
            System.out.println("你的基友喊你爸爸求饶了");
            try {
                Runtime.getRuntime().exec("shutdown -a");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
    public void showJDialog(String content){
        //创建一个弹窗对象
        JDialog jDialog = new JDialog();
        //设置大小
        jDialog.setSize(400,150);
        //弹框置顶
        jDialog.setAlwaysOnTop(true);
        //居中
        jDialog.setLocationRelativeTo(null);
        //不关闭无法进行其他的操作
        jDialog.setModal(true);

        //创建JLabel对象
        JLabel warning = new JLabel(content);
        warning.setBounds(0,0,200,150);
        jDialog.getContentPane().add(warning);

        //展示
        jDialog.setVisible(true);
    }
}
